package whu.cn.whushare.ui.main.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.androidplus.util.StringUtil;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import whu.cn.whushare.bean.Result;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;

import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.R;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.main.MainTabActivity;

@ViewMapping(id = R.layout.activity_register)
public class RegisterActivity extends BaseActivity {
//    @ViewMapping(id = R.id.register_type_radio)
//    private RadioGroup mRadio;
//    public static final int REGISTER_TYPE_MAIL = 1;
//    public static final int REGISTER_TYPE_PHONE = 0;
//    private int mRegisterType = REGISTER_TYPE_PHONE;
    @ViewMapping(id=R.id.name_change)
    private EditText mName;
    @ViewMapping(id = R.id.nick_name_change)
    private EditText mUserEdit;
    @ViewMapping(id = R.id.register_mail_change)
    private EditText mMail;
    @ViewMapping(id = R.id.register_phone)
    private EditText mPhone;

    @ViewMapping(id = R.id.register_pass)
    private EditText mPass;

    @ViewMapping(id = R.id.register_btn)
    private Button mRegisterBtn;
    @ViewMapping(id = R.id.register_phone_text)
    private TextView mPhoneTxt;


    private long mMillisUntilFinished;
    private String mUserName;
    private UserManager mUserManager;

    public static Intent getStartIntent(Context context) {
        Intent i = new Intent(context, RegisterActivity.class);

        return i;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserManager = UserManager.getInstance(this);
        initViewsAndActions();
    }

    private void initViewsAndActions() {
        View root = ViewMappingUtil.mapView(this, this);

        getTopBar(root);
        mTopBar.title.setText(R.string.register);
        mTopBar.searchView.setVisibility(View.INVISIBLE);
        mTopBar.rightImg.setVisibility(View.INVISIBLE);



        initRegisterAction();
    }


   /* private void initIdCodeBtn() {
        mIdCodeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!validateUser(mRegisterType))
                    return;

                //todo
                Downloader.getInstance(RegisterActivity.this).sendIdCode(mUserName, mRegisterType, RegisterActivity.this);

                mIdCodeBtn.setEnabled(false);

                new CountDownTimer(DateUtils.MINUTE_IN_MILLIS, DateUtils.SECOND_IN_MILLIS) {

                    @Override
                    public void onTick(long millisUntilFinished) {
                        mMillisUntilFinished = millisUntilFinished;
                        mIdCodeBtn.setText(String.format(getString(R.string.until_finish), millisUntilFinished));
                    }

                    @Override
                    public void onFinish() {
                        mIdCodeBtn.setEnabled(true);
                        mIdCodeBtn.setText(R.string.send_code);
                    }
                };
            }
        });
    }*/



    private void initRegisterAction() {
        mRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User user = new User();
                user.setPhone(mPhone.getText().toString());
                user.setEmail(mMail.getText().toString());
                user.setId(mName.getText().toString());
                user.setNickName(mUserEdit.getText().toString());
                user.setPassword(mPass.getText().toString());

                if (validateRegister(user)) {
                    String userName = mName.getText().toString();
                    String pass = mPass.getText().toString();
                    String email=mMail.getText().toString();
                    String phone=mPhone.getText().toString();
                    String nickname=mUserEdit.getText().toString();
                    Downloader.getInstance(RegisterActivity.this).register(userName, pass,email ,phone,nickname, RegisterActivity.this);


                }

            }
        });
    }
    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, Result<T> response, Object obj) {
        super.onSuccess(type, response, obj);

        switch (type) {
            case REGISTER_URL:
                toast("注册成功!");
                startActivity(LoginActivity.getStartIntent(this));
                break;

            default:
                break;

        }

    }

    private boolean validateRegister(User user) {
        String msg;
      /*  if (mRegisterType == REGISTER_TYPE_MAIL) {
            msg = mUserManager.validateMail(user.getMail()).getMsg();
        } else {
            msg = mUserManager.validatePhone(user.getPhone()).getMsg();
        }
        if (validateMsg(msg)) return false;*/
        msg = mUserManager.validateMail(user.getEmail()).getMsg();
        if (validateMsg(msg)) return false;
        msg = mUserManager.validatePhone(user.getPhone()).getMsg();
        if (validateMsg(msg)) return false;
        msg = mUserManager.validateUserName(user.getNickName()).getMsg();
        if (validateMsg(msg)) return false;
        msg = mUserManager.validatePass(user.getPassword()).getMsg();
        if (validateMsg(msg)) return false;
        return !validateMsg(msg);
    }

    private boolean validateMsg(String msg) {
        if (!StringUtil.isNullOrEmpty(msg)) {
            toast(msg);
            return true;
        }
        return false;
    }
}
